<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="导师编号">
              <a-input v-model="queryParam.dsbh"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="姓名">
              <a-input v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item label="院系">
                <a-select allowClear v-model="queryParam.dwh" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="导师状态">
                <a-select allowClear v-model="queryParam.shzt">
                  <a-select-option v-for="i in TYPE0320" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="是否在岗">
                <a-select allowClear v-model="queryParam.dssfzg">
                  <a-select-option v-for="i in TYPE0321" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="审核状态">
                <a-select allowClear v-model="queryParam.dsshzt">
                  <a-select-option value="1">审核中</a-select-option>
                  <a-select-option value="2">审核未通过</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="导师类别">
                <a-select mode="multiple" allowClear v-model="queryParam.dslbm">
                  <a-select-option v-for="i in TYPE0360" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="24" :sm="24">
            <span class="table-page-search-submitButtons">
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
            <a-button @click="queryParam = {};$refs.table.refresh()" style="margin-left: 8px;">重置</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :scroll="{ x: 1400 }"
        :rowSelection="options.rowSelection"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
      >
        <!-- :rowSelection="options.rowSelection" -->
        <!-- :alert="options.alert"
        -->
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <span slot="csrq" slot-scope="a">{{a && a.slice(0,11)}}</span>
        <!-- <a slot="dsbh" slot-scope="a, b" @click="direct(b)">{{a}}</a> -->
        <template slot="dsshzt" slot-scope="a,b">
          <a-tooltip placement="top">
            <template slot="title">
              <span> {{b.shsm}}</span>
            </template>
            <a-button>{{b.dsshzt==2?"审核中":"审核不通过"}}</a-button>
          </a-tooltip>
        </template>
      </s-table>
    </a-card>

    <check-modal
      ref="checkModal"
      :visible.sync="visible"
      @close="() => { visible = false }"
      @save="batchCheck"
    />
  </div>
</template>

<script>
  import {STable} from '~'
  import {mapState} from 'vuex'
  import checkModal from './component/checkModal'

  export default {
    data() {
      return {
        advanced: true,
        queryParam: {},
        columns: [
          {
            title: '序号',
            scopedSlots: {customRender: 'serial'}
          },
          {
            title: '导师编号',
            dataIndex: 'dsbh'
          },
          {
            title: '姓名',
            dataIndex: 'xm'
          },
          {
            title: '性别',
            dataIndex: 'xbmc'
          },
          {
            title: '出生日期',
            dataIndex: 'csrq',
            scopedSlots: {customRender: 'csrq'}
          },
          {
            title: '导师所属院系',
            dataIndex: 'dsssyxmc'
          },
          {
            title: '导师类别',
            dataIndex: 'dslbmc'
          },
          {
            title: '导师状态',
            dataIndex: 'shztmc'
          },
          {
            title: '是否在岗',
            dataIndex: 'dssfzgmc'
          },
          {
            title: '审核状态',
            dataIndex: 'dsshzt',
            scopedSlots: {customRender: 'dsshzt'},
          }
        ],
        parameter: {
          pageNo: 1,
          pageSize: 10
        },
        loadData: parameter => {
          this.parameter = parameter
          parameter.pageNum = parameter.pageNo
          let params = Object.assign(this.queryParam, parameter)
          return this.$api.teacher.teacherFailCheckList(params).then(res => {
            return {
              data: res.data.rows,
              pageNo: parameter.pageNo,
              pageSize: parameter.pageSize,
              totalPage: Math.ceil(res.data.total / parameter.pageSize),
              totalCount: res.data.total
            }
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        options: {
          alert: {
            show: true, clear: () => {
              this.selectedRowKeys = []
            }
          },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        },
        confirmLoading: false,
        selectMenu: {},
        visible: false,
        initDone: false
      }
    },
    components: {STable, checkModal},
    computed: {
      ...mapState({
        TYPE0360: state => state.app['TYPE0360'], // 导师类别
        TYPE0320: state => state.app['TYPE0320'], // 导师状态
        TYPE0133: state => state.app['TYPE0133'], // 是否在岗
        TYPE0321: state => state.app['TYPE0321'], // 是否在岗
        TYPE0133: state => state.app['TYPE0133'] // 招生学年
      })
    },
    beforeRouteEnter(to, from, next) {
      next(vm => {
        let pageParams = vm.$store.state.app.pageParams || {}
        if ((from.name === '导师信息审核页') && pageParams.path === vm.$route.path) {
          vm.queryParam = pageParams.params
          vm.parameter = pageParams.pagination
        }
        vm.initDone = true
      })
    },
    beforeRouteLeave(to, from, next) {
      if (to.name === '导师信息审核页') {
        this.$store.dispatch('app/setPageParams', {
          path: this.$route.path,
          params: this.queryParam,
          pagination: this.parameter
        })
      }
      next()
    },
    created() {
      this.initSelectMenu()
      this.$store.dispatch('app/setDictionary', 'TYPE0360')
      this.$store.dispatch('app/setDictionary', 'TYPE0320')
      this.$store.dispatch('app/setDictionary', 'TYPE0321')
      this.$store.dispatch('app/setDictionary', 'TYPE0133')
      this.$store.dispatch('app/setDictionary', 'TYPE0133')
    },
    methods: {
      initSelectMenu() {
        let allRequest = [
          this.$api.base.collegeAll()
        ]
        Promise.all(allRequest).then(res => {
          this.selectMenu = {
            yx: res[0].data
          }
        })
      },
      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      direct(data) {
        this.$router.push(`/teacher/info/${data.dsbh}/check`)
      },
      showCheck() {
        if (!this.selectedRowKeys.length) {
          this.$message.warning('请选择需要审核的导师')
          return
        }
        this.visible = true
      },
      filterOption(value, op) {
        return op.componentOptions.children[0].text.indexOf(value) !== -1
      },
      batchCheck(params) {
        this.$refs.checkModal.confirmLoading = true
        let dsbhs = this.selectedRows.map(ds => {
          return ds.dsbh
        })
        this.$api.teacher.teacherCheck(Object.assign({dsbhs: dsbhs.join(',')}, params)).then(() => {
          this.$message.success('操作成功')
          this.selectedRowKeys = []
          this.selectedRows = []
          this.visible = false
          this.$refs.table.refresh(false)
          this.$refs.table.clearSelected()
          this.$refs.checkModal.form.resetFields()
        }).finally(() => {
          this.$refs.checkModal.confirmLoading = false
        })
      }
    }
  }
</script>

<style>

</style>
